import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class CDStore {
    private final Map<Integer, Integer> sellableStock = new HashMap<>();
    private final boolean[] rentableCDs = new boolean[10];
    private final Lock sellLock = new ReentrantLock();
    private final Lock rentLock = new ReentrantLock();
    private boolean needsEmergencyRestock = false;

    public CDStore() {
        for (int i = 1; i <= 10; i++) {
            sellableStock.put(i, 10);
        }
    }

    public void restock() {
        sellLock.lock();
        try {
            for (int type : sellableStock.keySet()) {
                sellableStock.put(type, 10);
            }
        } finally {
            sellLock.unlock();
        }
    }

    public int getStock(int type) {
        sellLock.lock();
        try {
            return sellableStock.getOrDefault(type, 0);
        } finally {
            sellLock.unlock();
        }
    }

    public boolean sell(int type, int quantity) {
        sellLock.lock();
        try {
            int current = sellableStock.get(type);
            if (current >= quantity) {
                sellableStock.put(type, current - quantity);
                if (current - quantity == 0) {
                    needsEmergencyRestock = true;
                }
                return true;
            }
            return false;
        } finally {
            sellLock.unlock();
        }
    }

    public boolean rent(int cdNumber) {
        rentLock.lock();
        try {
            if (!rentableCDs[cdNumber - 1]) {
                rentableCDs[cdNumber - 1] = true;
                return true;
            }
            return false;
        } finally {
            rentLock.unlock();
        }
    }

    public void returnCD(int cdNumber) {
        rentLock.lock();
        try {
            rentableCDs[cdNumber - 1] = false;
        } finally {
            rentLock.unlock();
        }
    }

    public boolean needsEmergencyRestock() {
        sellLock.lock();
        try {
            return needsEmergencyRestock;
        } finally {
            sellLock.unlock();
        }
    }

    public void setNeedsEmergencyRestock(boolean needsRestock) {
        sellLock.lock();
        try {
            this.needsEmergencyRestock = needsRestock;
        } finally {
            sellLock.unlock();
        }
    }

    public void resetEmergencyFlag() {
        sellLock.lock();
        try {
            needsEmergencyRestock = false;
        } finally {
            sellLock.unlock();
        }
    }
}
